<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18 0018
 * Time: 15:30
 */

namespace Admin\Controller;

use Think\Controller;
class CommentsController extends Controller
{

    public function index(){
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;
        $comments=M('comments')->join('users on users.uid=comments.uid')->select();
        $this->comments=$comments;
        for ($i = 0; $i < count($comments); $i++){
            $comments[$i]["code"] = $i + 1;
        }
        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($comments){
            $this->assign('list', $comments);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/Comments/index"));}
    }


public function del(){
    $cid = $_GET['cid'];
    $comments = M("comments");
    $comments->cid = $cid;
    $comments->delete();

    if ($comments) {
        $this->success('删除成功');
    } else {
        $this->error('删除失败!');

    }


}

    public function search(){
        $text = trim(I('post.text'));
        $newsmodel=M('comments');
        $a['title']=array('like',"%{$text}%");
        $select=$newsmodel->where($a)->join('users on users.uid=comments.uid')->select();
        for ($i = 0; $i < count($select); $i++){
            $select[$i]["code"] = $i + 1;
        }
        if($select)
        {
            $this->assign('list', $select);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("Comments/index"));}
    }






}